package com.hsgene.logistics.controller;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.MessageResponse;
import com.hsgene.logistics.domain.LogisticsInfo;
import com.hsgene.logistics.dto.KuaiDi100Info;
import com.hsgene.logistics.service.LogisticsService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

;

/**
 * @Author: wangxuefeng@hsgene.com
 * @Description:
 * @Date: Created in 9:42 2018/7/20
 * @Modified By:
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/express")
public class LogisticsController {

    /**
     * LOG
     */
    private static Logger LOGGER = LogManager.getLogger(LogisticsController.class);

    @Autowired
    private LogisticsService logisticsService;

    /**
     * 新增物流信息
     *
     * @param info 物流信息
     * @return
     */
    @PostMapping
    public ApiResult<Integer> addLogistics(@RequestBody LogisticsInfo info) {
        try {
            Date date = new Date();
            info.setCreateDateTime(date);
            info.setUpdateDateTime(date);
            int result = logisticsService.insert(info);
            return new ApiResult(200, "success", "添加物流信息成功", result);
        } catch (Exception e) {
            LOGGER.error("add Logistics Info is error", e);
            return new ApiResult(MessageResponse.ADD_LOGISTICS_ERROR);
        }
    }

    /**
     * 查询物流信息
     *
     * @param orderId 订单号
     * @return
     */
    @GetMapping
    public ApiResult<List<LogisticsInfo>> queryLogistics(String orderId) {
        try {
            List<LogisticsInfo> result = logisticsService.queryLogisticsById(orderId);
            return new ApiResult(200, "success", "查询物流信息成功", result);
        } catch (Exception e) {
            LOGGER.error("query Logistics Info is error", e);
            return new ApiResult(MessageResponse.QUERY_LOGISTICS_ERROR);
        }
    }

    /**
     * 花生仁查询物流信息
     * @param provider 快递公司
     * @param expressNo 快递单号
     * @return
     */
    @GetMapping("delivery")
    public ApiResult<KuaiDi100Info> peanutLogisticsInfo(String provider, String expressNo){
        return logisticsService.peanutLogisticsInfo(provider,expressNo);
    }
}
